<?php

/**
 * Capital Direct Lending (http://www.philbsd.com/cdl)
 *
 * Copyright (C) 2008 Philippine Business Solution Developers <http://www.philbsd.com>
 * All rights reserved.
 *
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@philbsd.com so we can send you a copy immediately.
 *
 * @copyright  (c) 2008 Philippine Business Solution Developers. (http://www.philbsd.com)
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt
 * @version    $Id: payment.php 243 2008-05-01 16:37:25Z jundie2000 $
 */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Payment extends SB_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->_data['menuLoan'] = true;
        $this->load->model('Loans');
	}

	public function index()
	{
	    $this->_data['msg'] = $this->session->flashdata('msg');
		$this->_data['records'] = $this->Loans->getPaymentToday();
		$this->render('payment/index.shtml');

		if (isset($_POST['payment'])) {
			$payment = $_POST['payment'];  
		}

		if (isset($payment) && $payment) {

			for ($a=0; $a<count($payment); $a++) {

				$value 	= explode("&",$payment[$a]);
				$id = $value[0];
				$data['amount_paid'] = $value[1];
				$data['paid_date'] = date("Y-m-d");
             	$this->db->trans_begin();
            	$this->db->where('id', $id);
            	$this->db->update('payments', $data);
            	$this->db->trans_commit();
			}
            $this->session->set_flashdata('msg', "Successfully saved!");
        	header("Location: {$this->_data['baseUrl']}payment");
			exit;
		}
	}

	public function add($id)
	{
        $this->_data['record'] = $this->Loans->getLoan($id);
		$this->render('payment/add.shtml');
         // adding payments
         if (isset($_POST['Payment/loan_id'])) {
            $this->db->trans_begin();
			$data = $this->_swordPost['Payment'];
			$this->db->insert('payments', $data);
            $this->db->trans_commit();
            $this->session->set_flashdata('msg', "Successfully saved!");
            header("Location: {$this->_data['baseUrl']}/loan/view/{$id}.html");
			exit;
          }

	}
    public function edit($id)
	{
        $this->_data['record'] = $this->Loans->getPayment($id);
		$this->render('payment/edit.shtml');
         // editting payments
         if (isset($_POST['Payment/loan_id'])) {
            $this->db->trans_begin();
			$data = $this->_swordPost['Payment'];
            $this->db->where('id', $id);
            $this->db->update('payments', $data);
            $this->db->trans_commit();
            $this->session->set_flashdata('msg', "Successfully saved!");
            header("Location: {$this->_data['baseUrl']}/loan/view/{$data['loan_id']}.html");
			exit;
          }
	}
    function popadd($loan_id)
	{
                $this->_data['loan_id'] = $loan_id;
                $this->_data['record'] = $this->Loans->getLoan($loan_id);
                if ($this->input->post('Payment/loan_id')) { 
                 	$this->_data['loan_id'] = $this->input->post('Payment/loan_id');
                }                                           
                $this->load->helper(array('form', 'url'));
                $this->load->library('validation');

                if (isset($_POST['Payment/loan_id'])) {

                        $rules['Payment/amount_due'] = "required";
                        $rules['Payment/due_date'] = "required";
                        $rules['Payment/amount_paid'] = "required";
                        $rules['Payment/paid_date'] = "required";
                        $this->validation->set_rules($rules);

                        $fields['Payment/amount_due'] = 'Amount Due';
                        $fields['Payment/due_date'] = 'Due Date';
                        $fields['Payment/amount_paid'] = 'Amount Paid';
                        $fields['Payment/paid_date'] = 'Paid Date';
                        $this->validation->set_fields($fields);

                        if ($this->validation->run()) {
                                $this->db->trans_begin();

                                // adding business
                                $data = $this->_swordPost['Payment'];
                                $this->db->insert('payments', $data);
                                $noteId = $this->db->insert_id();

                                if ($this->db->trans_status() === FALSE) {
                                        $this->db->trans_rollback();
                                        $errMsg = "Failed to add!";
                                        if (isset($this->_data['msg'])) {
                                                $this->_data['msg'] .= $errMsg;
                                        } else {
                                                $this->_data['msg'] = $errMsg;
                                        }
                                } else {
                                        $this->db->trans_commit();
                                        $this->_data['success_msg'] = "Successfully saved.";
                                        $this->_data['my_id'] = $noteId;
                                }

                        }
                }

                $this->render('payment/popadd.shtml', array(), 'popup.shtml');
	}

    function popdel($payment_id)
	{
		$this->_data['payment_id'] = $payment_id;
		if ($this->input->post('payment_id')) {
			$this->db->delete('payments', array('id' => $this->input->post('payment_id')));
			$this->_data['success_msg'] = "Successfully deleted.";
		}
		$this->render('payment/popdel.shtml', array(), 'popup.shtml');
	}

	function deletepayment($payment_id)
	{
			$data = $this->Loans->getPayment($payment_id);
			$this->db->delete('payments', array('id' => $payment_id));
			$this->_data['success_msg'] = "Successfully deleted.";
			header("Location: {$this->_data['baseUrl']}loan/view/{$data['loan_id']}.html");
			exit;
	}

}